home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
ctimer
/
timer.c
Wrap
C/C++ Source or Header
|
1987-12-11
|
2KB
|
75 lines
/*============Demonstration of interrupt handling in MSC ver 5.0=============
TIMER.C was created to test the use of interrupt handling in Microsoft
C 5.0. Timer is a functional program which can produce a timed delay
from a DOS command. To use TIMER enter:
TIMER delay Where: delay = Seconds to wait
*/
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#define TIMER 8 /* Timer interrupt number */
void (_CDECL interrupt far *original)(); /* Pointer to original handler */
volatile unsigned ticks;
/*==========================Timer interrupt handler==========================*/
void _CDECL interrupt far tick(void);
void _CDECL interrupt far tick()
{
int a; /* The "int a;" declaration is a work around for an
optimization bug in MSC 5.0 where it unwittingly
removes code to save the stack pointer, not
knowing that _chain_intr() is about to be used.
Any automatic variable will prevent problems. */
ticks++; /* Count one tick */
_chain_intr( original ); /* This calls the original handler */
}
/*===================================Main====================================*/
main( argc, argv )
int argc;
char **argv;
{
unsigned delay;
/* Test usage and use bdos() to avoid bringing in
a whole lot of code with the prinf() function */
if( argc!=2 || (delay=atoi(argv[1]))==0 )
{
bdos( 9, (unsigned)"Usage: TIMER delay\r\n$", 0 );
abort();
}
/* The timer interrupt occurs 18.2065 times/sec.
The form below avoids using floating math. */
delay = (delay*182)/10; /* Calculate # of ticks */
/* Install our tick() handler */
original = _dos_getvect( TIMER ); /* save original for chain */
_dos_setvect( TIMER, tick ); /* then install tick() */
/* This loop watches the value "ticks" which
increments in the background at each timer
interrupt. Compiling with loop optimization
will cause the compiler to screw this up. */
for( ticks=0 ; ticks<delay ; ); /* Count STOP ticks */
/* Remove our tick() handler by replacing
the original interrupt vector. */
_dos_setvect( TIMER, original ); /* Remove INT 8 handler */
exit(0);
}